<?php
namespace Model\Loader;

use Symfony\Component\DomCrawler\Crawler;

/**
 * Description of AbstractLoader
 *
 * @author pahhan
 */
abstract class AbstractLoader implements LoaderInterface
{
    protected $options = array(
        'charset' => 'UTF-8',
    );

    public function __construct(array $options = array())
    {
        $this->options = array_merge($this->options, $options);
        if( empty($this->options['charset']) ) {
            $this->options['charset'] = 'UTF-8';
        }
    }

    public function setOption($name, $value)
    {
        $this->options[$name] = $value;
    }

    public function getSource($url, $charset)
    {
        $html = $this->getRawHtml($url);
        return mb_convert_encoding($html, $this->options['charset'], $charset);
    }

    abstract protected function getRawHtml($url);

    public function getCrawler($url, $charset)
    {
        $html = $this->getSource($url, $charset);
        $crawler = new Crawler(NULL, $url);
        $crawler->addHtmlContent($html, $this->options['charset']);
        return $crawler;
    }
}

